package com.bite.system.service.question.impl;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bit.common.security.handler.ServiceException;
import com.bite.common.core.contants.Constants;
import com.bite.common.core.enums.ResultCode;
import com.bite.system.domain.question.Question;
import com.bite.system.domain.question.dto.QuestionAddDTO;
import com.bite.system.domain.question.dto.QuestionQueryDTO;
import com.bite.system.domain.question.es.QuestionES;
import com.bite.system.domain.question.vo.QuestionDetailVO;
import com.bite.system.domain.question.vo.QuestionVO;
import com.bite.system.domain.question.dto.QuestionEditDTO;
import com.bite.system.elasticsearch.QuestionRepository;
import com.bite.system.manager.QuestionCacheManager;
import com.bite.system.mapper.question.QuestionMapper;
import com.bite.system.service.question.IQuestionService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class QuestionServiceImpl implements IQuestionService {

    @Autowired
    private QuestionMapper questionMapper;


    @Autowired
    private QuestionRepository questionRepository;

    @Autowired
    private QuestionCacheManager questionCacheManager;

    @Override
    public List<QuestionVO> list(QuestionQueryDTO questionQueryDTO) {

        String excludeIdStr = questionQueryDTO.getExcludeIdStr();

        if (StrUtil.isNotEmpty(excludeIdStr)){
            //不为空解析
            String[] excludeIdArr =excludeIdStr.split(Constants.SPLIT_SEM);  //对内容进行解析
            //既转化为Long 又去重
            Set<Long> excludeIdSet = Arrays.stream(excludeIdArr)
                    .map(Long::valueOf)
                    .collect(Collectors.toSet());
            questionQueryDTO.setExcludeIdSet(excludeIdSet);
        }

        PageHelper.startPage(questionQueryDTO.getPageNum(),questionQueryDTO.getPageSize());
        List<QuestionVO> questionVOList = questionMapper.selectQuestionList(questionQueryDTO);//当前符合条件的
        return questionVOList;

    }

    @Override
    public boolean add(QuestionAddDTO questionAddDTO) {
        List<Question> questionList = questionMapper.selectList(new LambdaQueryWrapper<Question>().eq(Question::getTitle, questionAddDTO.getTitle()));
        if (!CollectionUtil.isEmpty(questionList)){
            throw new ServiceException(ResultCode.FAILED_ALREADY_EXISTS);
        }
        //把对象转化为实体
        Question question = new Question();
        BeanUtils.copyProperties(questionAddDTO,question);  //hutool提供的转化方法
        //生成创建人,创建时间--
        int insert = questionMapper.insert(question);
        if (insert <= 0){
            return false;
        }
        //插数据库成功
        //将数据插入es
        QuestionES questionES = new QuestionES();
        BeanUtil.copyProperties(question,questionES);
        questionRepository.save(questionES);

        //缓存放
        questionCacheManager.addCache(question.getQuestionId());
        return true;

    }



    @Override
    public QuestionDetailVO detail(Long questionId) {
        Question question = questionMapper.selectById(questionId);
        if (question == null){
            throw new ServiceException(ResultCode.FAILED_NOT_EXISTS);
        }
        //实体转为VO
        QuestionDetailVO questionDetailVO = new QuestionDetailVO();
        BeanUtils.copyProperties(question,questionDetailVO);  //hutool提供的转化方法
        return questionDetailVO;
    }

    @Override
    public int edit(QuestionEditDTO questionEditDTO) {
        Question oldQuestion = questionMapper.selectById(questionEditDTO.getQuestionId());
        if (oldQuestion == null){
            throw new ServiceException(ResultCode.FAILED_NOT_EXISTS);
        }

        oldQuestion.setTitle(questionEditDTO.getTitle());
        oldQuestion.setDifficulty(questionEditDTO.getDifficulty());
        oldQuestion.setSpaceLimit(questionEditDTO.getSpaceLimit());
        oldQuestion.setTimeLimit(questionEditDTO.getTimeLimit());
        oldQuestion.setContent(questionEditDTO.getContent());
        oldQuestion.setQuestionCase(questionEditDTO.getQuestionCase());
        oldQuestion.setMainFuc(questionEditDTO.getMainFuc());

        //同步es
        QuestionES questionES = new QuestionES();
        BeanUtil.copyProperties(oldQuestion,questionES);
        questionRepository.save(questionES);

        int row =questionMapper.updateById(oldQuestion);
        return row;

    }


    @Override
    public int delete(Long questionId) {
        Question question = questionMapper.selectById(questionId);
        if (question == null){
            throw new ServiceException(ResultCode.FAILED_NOT_EXISTS);
        }
        questionRepository.deleteById(questionId);
        //缓存中删除
        questionCacheManager.deleteCache(questionId);
        return questionMapper.deleteById(questionId);

    }


}
